﻿using System;
using System.Collections.Generic;
using System.Linq;

// 订单明细类
class OrderDetails
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public OrderDetails(string productName, double price, int quantity)
    {
        ProductName = productName;
        Price = price;
        Quantity = quantity;
    }
}

// 订单类
class Order
{
    public string OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetails> Details { get; set; }

    public double TotalAmount
    {
        get
        {
            return Details.Sum(d => d.Price * d.Quantity);
        }
    }

    public Order(string orderId, string customer, List<OrderDetails> details)
    {
        OrderId = orderId;
        Customer = customer;
        Details = details;
    }

    public override string ToString()
    {
        return $"订单号：{OrderId}\t客户：{Customer}\t订单金额：{TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders;

    public OrderService()
    {
        orders = new List<Order>();
    }

    public void AddOrder(Order order)
    {
        orders.Add(order);
    }

    public void RemoveOrder(string orderId)
    {
        Order order = GetOrderById(orderId);
        if (order != null)
        {
            orders.Remove(order);
        }
    }

    public void ModifyOrder(string orderId, string newCustomer)
    {
        Order order = GetOrderById(orderId);
        if (order != null)
        {
            order.Customer = newCustomer;
        }
    }

    public Order GetOrderById(string orderId)
    {
        return orders.FirstOrDefault(o => o.OrderId == orderId);
    }

    public List<Order> QueryOrdersByProductName(string productName)
    {
        return orders.Where(o => o.Details.Any(d => d.ProductName == productName)).ToList();
    }

    public List<Order> QueryOrdersByCustomer(string customer)
    {
        return orders.Where(o => o.Customer == customer).ToList();
    }

    public List<Order> QueryOrdersByTotalAmount(double amount)
    {
        return orders.Where(o => o.TotalAmount == amount).ToList();
    }

    public void DisplayOrders()
    {
        foreach (Order order in orders)
        {
            Console.WriteLine(order);
        }
    }
}

class Program
{
    static void Main()
    {
        OrderService orderService = new OrderService();

        // 添加订单
        Order order1 = new Order("001", "Customer1", new List<OrderDetails>
        {
            new OrderDetails("Product1", 10, 2),
            new OrderDetails("Product2", 20, 3)
        });
        orderService.AddOrder(order1);

        Order order2 = new Order("002", "Customer2", new List<OrderDetails>
        {
            new OrderDetails("Product1", 10, 1),
            new OrderDetails("Product3", 30, 2)
        });
        orderService.AddOrder(order2);

        Order order3 = new Order("003", "Customer1", new List<OrderDetails>
        {
            new OrderDetails("Product2", 20, 4),
            new OrderDetails("Product3", 30, 1)
        });
        orderService.AddOrder(order3);

        // 显示所有订单
        Console.WriteLine("所有订单：");
        orderService.DisplayOrders();

        // 删除订单
        orderService.RemoveOrder("002");
        Console.WriteLine("删除订单后的结果：");
        orderService.DisplayOrders();

        // 修改订单
        orderService.ModifyOrder("001", "New Customer");
        Console.WriteLine("修改订单后的结果：");
        orderService.DisplayOrders();

        // 按订单号查询订单
        Console.WriteLine("按订单号查询订单：");
        Order queryOrder = orderService.GetOrderById("003");
        if (queryOrder != null)
        {
            Console.WriteLine(queryOrder);
        }

        // 按商品名称查询订单
        Console.WriteLine("按商品名称查询订单：");
        List<Order> queryByProductName = orderService.QueryOrdersByProductName("Product2");
        foreach (Order order in queryByProductName)
        {
            Console.WriteLine(order);
        }

        // 按客户查询订单
        Console.WriteLine("按客户查询订单：");
        List<Order> queryByCustomer = orderService.QueryOrdersByCustomer("Customer1");
        foreach (Order order in queryByCustomer)
        {
            Console.WriteLine(order);
        }

        // 按订单金额查询订单
        Console.WriteLine("按订单金额查询订单：");
        List<Order> queryByTotalAmount = orderService.QueryOrdersByTotalAmount(60);
        foreach (Order order in queryByTotalAmount)
        {
            Console.WriteLine(order);
        }

        Console.ReadLine();
    }
}